<template>
  <div class="login-container">
    <el-alert v-show="false" title="后台管理" type="success" :closable="false" style="position: fixed"></el-alert>
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="14" :xl="14">
        <div style="color: transparent">左侧区域占位符</div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="10" :xl="10">
        <el-form status-icon class="login-form">
          <div class="form-header">
            <div class="title">hello !</div>
            <div class="title-tips">
              欢迎来到后台管理！
            </div>
          </div>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username" autocomplete="off" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password" autocomplete="off" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item prop="captchaCode">
            <el-row>
              <el-col :span="15">
                <el-input autocomplete="off" v-model="loginForm.captchaCode" placeholder="请输入验证码" />
              </el-col>
              <el-col :span="8" :offset="1">
                <el-image fit="contain" :src="image" @click="parseCaptcha()" style="height: 100%; background: white" class="pointer" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-button class="login-btn" @click="parseLogin()" type="primary" size="large">
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">

import { User, Lock } from '@element-plus/icons-vue'
//-----------登录方法
//1. 导入依赖 (userStore , LoginType , useRouter)
import userStore from '@/store/modules/login/userStore'
import { useRouter} from 'vue-router' 
//从下面挪上来的
import {getCaptcha , LoginType} from '@/api/login/index'
import { ElNotification } from 'element-plus'

//2.准备数据 (请求参数) 
// username: "", password: "", captchaCode: "" ->双向绑定收集表单数据  v-model="loginForm.username"
// captchaKey: "", 验证码接口赋值
let loginForm = ref<LoginType>({
    username: "",
    password: "",
    captchaKey: "",
    captchaCode: ""
});

//3.调用方法
let user = userStore();
let router = useRouter();

let parseLogin = async()=>{
   await user.doLogin(loginForm.value);
   ElNotification({
    title: '登录成功',
    message: '欢迎光临!!!',
    type: 'success',
  })
   //成功以后,再跳转页面
   router.push("/");
}

//------------验证码
//1.导入依赖
//import {getCaptcha} from '@/api/login/index.ts'
import {ref , onMounted} from 'vue'

//2.准备响应式数据
let image = ref("");

//3.定义(解析)调用方法 (onMounted中直接await | 不方便后续切换调用)
let parseCaptcha = async ()=>{
   let {code,data} = await getCaptcha();
   if(code == 200){
     image.value = data.image;
     //给与key
     loginForm.value.captchaKey = data.key;
   }
}


//4.初始化调用
onMounted(()=>{
  parseCaptcha();
})

//5.展示数据

</script>

<style scoped lang="scss">
@import './index';
</style>
